package gui;

import gestionBDD.requetesClient;
import interne.Client;
import java.sql.*;

import com.trolltech.qt.gui.*;

public class fenetreAccueil extends QMainWindow {
	
    private QLineEdit loginLineEdit;
    private QLineEdit passwordLineEdit;
    
	public fenetreAccueil() throws SQLException {

        /** Login's GroupBox' **/
        QGroupBox loginGroup = new QGroupBox(tr("Espace client :"));
 
        /* Login Line Edit avec Label */
        loginLineEdit = new QLineEdit("E-Mail");
        loginLineEdit.setFocus();
        
        /* Password LineEdit avec Label */
        passwordLineEdit = new QLineEdit("Password");
        passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password);
        
        /* Bouton Connexion */
        QPushButton boutonConnexion = new QPushButton("Se connecter");
        boutonConnexion.clicked.connect(this, "connecter()");        
      
        /* Login's Layout */
        QVBoxLayout loginLayout = new QVBoxLayout();
        loginLayout.addWidget(loginLineEdit);
        loginLayout.addWidget(passwordLineEdit);
        loginLayout.addWidget(boutonConnexion) ;
        loginGroup.setLayout(loginLayout);
        
        /** Visiteur GroupBox' **/
        QGroupBox visiteurGroup = new QGroupBox(tr("Nouveau Client ?"));
        
        /* Bouton Insciption */
        QPushButton boutonInscription = new QPushButton("S'inscrire");
        boutonInscription.clicked.connect(this, "popInscription()");        

        /* Bouton Albums publics */
        QPushButton boutonAlbumsPublics = new QPushButton("Voir les albums publics");
        boutonAlbumsPublics.clicked.connect(this, "consulterAlbumsPub()"); 
        
        /* Bouton Albums publics */
        QPushButton boutonAdmin = new QPushButton("Panneau administrateur");
        boutonAdmin.clicked.connect(this, "menuAdmin()"); 
        
        /* Visiteur's Layout */
        QVBoxLayout visiteurLayout = new QVBoxLayout();
        visiteurLayout.addWidget(boutonInscription) ;
        visiteurLayout.addWidget(boutonAlbumsPublics) ;
        visiteurLayout.addWidget(boutonAdmin) ;
        visiteurGroup.setLayout(visiteurLayout) ;
        
        /* Principal Layout */
        QGroupBox principalGroup = new QGroupBox() ;
        QVBoxLayout principal = new QVBoxLayout() ;
        principal.addWidget(loginGroup);
        principal.addWidget(visiteurGroup);
        principalGroup.setLayout(principal);
 
        resize(400, 500);
        setCentralWidget(principalGroup) ;
        setWindowTitle(tr("PhotoNum"));        
	}

		/*
		 * Affichage de la fenêtre inscription
		 */
		public void popInscription() {
			fenetreInscription fenetre = new fenetreInscription() ;
			fenetre.show();
		}
		
		/*
		 * Vérifie l'authentification du client et charge l'espace client
		 */
	    public void connecter() throws SQLException {
	    	try {
	    		Client c = new Client(loginLineEdit.text(), passwordLineEdit.text()) ;
	    		Boolean succesConnexion = requetesClient.connecterClient(c) ;
	    		if (succesConnexion) {
		            fenetreClient fenetre = new fenetreClient(c);
		    		this.setCentralWidget(fenetre) ;
	    		}
	    		else {
	    			QMessageBox popErreur = new QMessageBox() ;
	    			popErreur.setWindowTitle("Problem?") ;
	    			popErreur.setText("Les identifiants sont incorrects.");
	    			popErreur.show() ;
	    		}
	    	}
	    	catch (SQLException e) {
	    		System.out.println(e) ;
	    	}
	    }
	    
	    /*
	     * Accès aux albums publics
	     */
	    public void consulterAlbumsPub() throws SQLException {
	    	try {
	            fenetreAlbumPub fenetre = new fenetreAlbumPub(null);
	    		this.setCentralWidget(fenetre) ;
	    	}
	    	catch (SQLException e) {
	    		System.out.println(e) ;
	    	}
	    }
	    
	    /*
	     * Accès au menu administration
	     */
	    public void menuAdmin() {
	    	fenetreAdmin fenetre = new fenetreAdmin();
			this.setCentralWidget(fenetre) ;
	    }
}
